package com.fjh.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fjh.dao.ProEsDao;
import com.fjh.es.ProdEs;
import com.fjh.service.SearchService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import cn.hutool.core.bean.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@Api(tags = "搜索管理")
public class SearchController {

    @Autowired
    private SearchService searchService;

    @Autowired
    private ProEsDao proEsDao;

    /**
     * 对外提供三组接口
     *
     */

    @GetMapping("prod/prodListByTagId")
    @ApiOperation("根据分组id查询es")
    public ResponseEntity<Page<ProdEs>> prodListByTagId(Long tagId,
                                                        @RequestParam(defaultValue = "6",required = false)Integer size,
                                                        @RequestParam(defaultValue = "0",required = false)Integer page){
        Page<ProdEs> prodEsPage = searchService.searchProdByTagId(tagId,size,page);
        return ResponseEntity.ok(prodEsPage);
    }

    @GetMapping("prod/searchProdPage")
    @ApiOperation("根据关键字查询es的商品")
    public ResponseEntity<Page<ProdEs>> searchProdPage(String prodName,Integer size,Integer current,Integer sort){
        Page<ProdEs> prodEsPage = searchService.searchProdByKeywords(prodName,current,size,sort);
        return ResponseEntity.ok(prodEsPage);
    }

    @PostMapping("search/getProdEsByIds")
    List<ProdEs> getProdEsByIds(@RequestBody List<Long> prodIds){
        Iterable<ProdEs> prodEs = proEsDao.findAllById(prodIds);
        List<ProdEs> prodEsArrayList = new ArrayList<>();
        prodEs.forEach(p->{
            ProdEs prodEs1 = new ProdEs();
            BeanUtil.copyProperties(p,prodEs1,true);
            prodEsArrayList.add(prodEs1);
        });
        return prodEsArrayList;
    }


}
